package map.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import map.Airport;

import core.Graphe;
import core.compagnie.CarteFidelite;
import core.compagnie.TypeCarteFidelite;
import core.dijkstra.DijkstraEngine;
import core.voyageur.Client;
import core.voyageur.Trajet;

import serveur.SimulationServeur;
 
public class FenetreNewClient extends JFrame implements ActionListener {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JPanel pan;
	private SimulationServeur serveur;
	private JButton buttonCreer;
	private JButton buttonAddCarte;
	private JButton buttonRemoveCarte;
	private JPanel northPanel;
	private JPanel centerPanel;
	private JPanel southPanel;
	private JTextField nom;
	private JTextField prenom;
	private JComboBox cbCartes;
	private JList listeCartes;
	private Hashtable<String, TypeCarteFidelite> cartes;

	
        public FenetreNewClient(String titre, int width, int height, SimulationServeur serveur){

        		this.serveur=serveur;
                this.setTitle(titre);
                this.setSize(width, height);
                this.setLocationRelativeTo(null);             
                
                nom = new JTextField();
                prenom = new JTextField();
                cartes = new Hashtable<String, TypeCarteFidelite>();
                
                //Instanciation d'un objet JPanel
                pan = new JPanel(new BorderLayout());       
                //On prévient notre JFrame que ce sera notre JPanel qui sera son contentPane
                this.setContentPane(pan);
    			
    			buttonCreer = new JButton("Créer");
    			buttonCreer.setEnabled(true);
    			buttonCreer.addActionListener(this);
    			
    			GridLayout layout = new GridLayout(2,1);
    			
    			/* Partie haute :
    			 * Infos client
    			 */
    			northPanel = new JPanel(layout);
    			
    			northPanel.add(new JLabel("Nouveau client :"), BorderLayout.NORTH);
    			
    			JPanel infosPanel = new JPanel();
    			infosPanel.setLayout(new BoxLayout(infosPanel, BoxLayout.PAGE_AXIS));
    			
    			JPanel nomPanel = new JPanel();
    			nomPanel.add(new JLabel("Nom :"));
    			nom.setEnabled(true);
    			nom.setPreferredSize(new Dimension(150, 25));
    			nomPanel.add(nom);
    			
    			
    			JPanel prenomPanel = new JPanel();
    			prenomPanel.add(new JLabel("Prenom :"));
    			prenom.setEnabled(true);
    			prenom.setPreferredSize(new Dimension(150, 25));
    			prenomPanel.add(prenom);
    			
    			
    			JPanel addCartesPanel = new JPanel();
    			addCartesPanel.add(new JLabel("Cartes fidélités :"));
    			cbCartes = new JComboBox(serveur.getTypesCartes().toArray());
    			addCartesPanel.add(cbCartes);
    			
    			buttonAddCarte = new JButton("Ajouter");
    			buttonAddCarte.setEnabled(true);
    			buttonAddCarte.addActionListener(this);
    			addCartesPanel.add(buttonAddCarte);
    			
    			JPanel cartesPanel = new JPanel();
    			listeCartes = new JList();
    			listeCartes.setPreferredSize(new Dimension(300,100));
    			cartesPanel.add(listeCartes);
    			
       			buttonRemoveCarte = new JButton("Supprimer");
       			buttonRemoveCarte.setEnabled(true);
       			buttonRemoveCarte.addActionListener(this);
    			cartesPanel.add(buttonRemoveCarte);
    			   			
    			infosPanel.add(nomPanel, BorderLayout.WEST);
    			infosPanel.add(prenomPanel, BorderLayout.WEST);
    			infosPanel.add(addCartesPanel, BorderLayout.WEST);
    			infosPanel.add(cartesPanel);
    					    		
    			centerPanel = new JPanel();
    			centerPanel.add(infosPanel, BorderLayout.WEST);
    			
    			
    			southPanel = new JPanel();
    			southPanel.add(buttonCreer);
    						

    			pan.add(northPanel, BorderLayout.NORTH);
    			pan.add(centerPanel, BorderLayout.WEST);
    			pan.add(southPanel, BorderLayout.SOUTH);
                
                this.setVisible(true);
        }

		public JPanel getPan() {
			return pan;
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			if(e.getActionCommand().equals(buttonCreer.getActionCommand())) {
				if(nom.getText().isEmpty() || prenom.getText().isEmpty()) {
					JOptionPane.showMessageDialog(pan, "Vous devez obligatoirement renseigner les champs Nom et Prénom !", "Attention !", JOptionPane.ERROR_MESSAGE);
					if(nom.getText().isEmpty()) nom.requestFocus();
					else prenom.requestFocus();
				} else {
					Client c = new Client(nom.getText(), prenom.getText());
					Iterator<TypeCarteFidelite> ittype = cartes.values().iterator();
					while(ittype.hasNext()) {
						CarteFidelite carte = new CarteFidelite(c, ittype.next());
						c.addCarte(carte);
					}
					serveur.addClient(c);

					this.dispose();
				}
			
			}
			
			if(e.getActionCommand().equals(buttonAddCarte.getActionCommand())) {		
				cartes.put(((TypeCarteFidelite)cbCartes.getSelectedItem()).getCompagnie().getCode(),(TypeCarteFidelite)cbCartes.getSelectedItem());
				listeCartes.setListData(cartes.values().toArray());
				listeCartes.validate();
			}
			
			if(e.getActionCommand().equals(buttonRemoveCarte.getActionCommand())) {	
				if(listeCartes.getSelectedValues().length != 0) {
					for(Object o : listeCartes.getSelectedValues()) cartes.remove(((TypeCarteFidelite)o).getCompagnie().getCode());
					listeCartes.setListData(cartes.values().toArray());
					listeCartes.validate();
				}
			}
			
		}

        
        
        
}